package com.hc.testaudio.deletea.test1;

/**
 * 用于缓存采集到的数据
 */
public class DataPoolUtil {
    //    private List<Byte> mDataPoll = new ArrayList<>();
    private final int DATA_POLL_MAX_SIZE = Integer.MAX_VALUE / 1024;
    private int[] mDataPoll = new int[DATA_POLL_MAX_SIZE];
    private int mDataIndexStart = 0;
    private int mDataIndexEnd = 0;
    private int mGetDataSize = 16384;//16384
    private GetDataCall mGetDataCall;
    private int mSamplingRate;

    public int getSamplingRate() {
        return mSamplingRate;
    }

    public void setSamplingRate(int samplingRate) {
        this.mSamplingRate = samplingRate;
    }


    public DataPoolUtil() {
    }

    public DataPoolUtil(int getDataSize) {
        this.mGetDataSize = getDataSize;
    }

    public synchronized void add2(byte[] newData) {
        int newDataLength = newData.length;
        for (int i = 0; i < newDataLength; i++) {
            mDataPoll[mDataIndexEnd + i] = newData[i];
        }
        mDataIndexEnd += newDataLength;
        int mDataLength;
        if (mDataIndexEnd < mDataIndexStart) {
            mDataLength = DATA_POLL_MAX_SIZE - mDataIndexStart + mDataIndexEnd;
        } else {
            mDataLength = mDataIndexEnd - mDataIndexStart;
        }
        if (mDataLength >= mGetDataSize) {
            int[] getData = new int[mGetDataSize];
            for (int i = 0; i < mGetDataSize; i++) {
                getData[i] = mDataPoll[mDataIndexStart + i];
            }
            mDataIndexStart += mGetDataSize;
            mGetDataCall.getData(getData, mSamplingRate, mGetDataSize);
        }
    }

    public synchronized void add(byte[] newData) {
        synchronized (this) {
            int newDataLength = newData.length;
            if (newDataLength + mDataIndexEnd <= DATA_POLL_MAX_SIZE) {//判断是否够存
                byte temp;
                for (int i = 0; i < newDataLength; i++) {
                    if (newData[i] == 0 || newData[i] == -128) {
                        temp = 0;
                    } else if (newData[i] > 0) {
                        temp = (byte) (128 - newData[i]);
                    } else {
                        temp = (byte) ((newData[i] + 128) * -1);
                    }
                    mDataPoll[mDataIndexEnd + i] = temp;
                }
                mDataIndexEnd += newDataLength;
            } else {//不够时，从0重新开始存
                int newDataLength1 = DATA_POLL_MAX_SIZE - mDataIndexEnd;
                byte temp;
                for (int i = 0; i < newDataLength1; i++) {
                    if (newData[i] == 0 || newData[i] == -128) {
                        temp = 0;
                    } else if (newData[i] > 0) {
                        temp = (byte) (128 - newData[i]);
                    } else {
                        temp = (byte) ((newData[i] + 128) * -1);
                    }
                    mDataPoll[mDataIndexEnd + i] = temp;
                }
                int newDataLength2 = newDataLength - newDataLength1;
                mDataIndexEnd = 0;
                for (int i = 0; i < newDataLength2; i++) {
                    if (newData[i] == 0 || newData[i] == -128) {
                        temp = 0;
                    } else if (newData[i] > 0) {
                        temp = (byte) (128 - newData[i]);
                    } else {
                        temp = (byte) ((newData[i] + 128) * -1);
                    }
                    mDataPoll[mDataIndexEnd + i] = temp;
                }
                mDataIndexEnd += newDataLength2;
            }
            int mDataLength;
            if (mDataIndexEnd < mDataIndexStart) {
                mDataLength = DATA_POLL_MAX_SIZE - mDataIndexStart + mDataIndexEnd;
            } else {
                mDataLength = mDataIndexEnd - mDataIndexStart;
            }
            if (mDataLength >= mGetDataSize) {
                int[] getData = new int[mGetDataSize];
                for (int i = 0; i < mGetDataSize; i++) {
                    getData[i] = mDataPoll[mDataIndexStart + i];
                }
                mDataIndexStart += mGetDataSize;
                mGetDataCall.getData(getData, mSamplingRate, mGetDataSize);
            }
        }
//        synchronized (this) {
//            if (mGetDataCall == null) {
//                YcLog.e("缓存的数据的获取回调为空!!!");
//                return;
//            }
//            for (byte addData : newData) {
//                mDataPoll.add(addData);
//            }
//            update();
//        }
    }
//
//    private synchronized void update() {
//        if (mDataPoll.size() >= mGetDataSize) {
//            byte[] data = new byte[mGetDataSize];
//            for (int i = mGetDataSize - 1; i >= 0; i--) {
//                data[i] = mDataPoll.get(i);
//                mDataPoll.remove(i);
//            }
//            mGetDataCall.getData(data, mSamplingRate, mCaptureSize);
//        }
//    }

    public void setGetDataCall(GetDataCall getDataCall) {
        this.mGetDataCall = getDataCall;
    }

    public static interface GetDataCall {
        void getData(int[] byteData, int samplingRate, int captureSize);
    }
}
